package com.atguigu03.threadsafe.threadsafe;

class SaleTickets extends Thread {


    static int tickets = 100;
    @Override
    public void run() {
        while (true) {

            synchronized (SaleTickets.class) {
                if(tickets > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + tickets);
                    tickets--;
                } else {
                    break;
                }
            }
        }
    }
}


public class TicketsTest {
    public static void main(String[] args) {

        SaleTickets t1 = new SaleTickets();
        SaleTickets t2 = new SaleTickets();
        SaleTickets t3 = new SaleTickets();

        t1.start();
        t2.start();
        t3.start();


    }
}
